package com.wingsoft.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wingsoft.common.utils.IdGenerator.IdGeneratorFactory;
import com.wingsoft.common.utils.StringUtils;
import com.wingsoft.system.domain.SysOperLog;
import com.wingsoft.system.mapper.SysOperLogMapper;
import com.wingsoft.system.service.ISysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 操作日志 服务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysOperLogServiceImpl implements ISysOperLogService
{
    @Autowired
    private SysOperLogMapper operLogMapper;

    @Autowired
    private IdGeneratorFactory idGeneratorFactory;
    @Autowired
    private SysOperLogMapper sysOperLogMapper;

    /**
     * 新增操作日志
     * 
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLog operLog)
    {
//        operLogMapper.insertOperlog(operLog);
        operLog.setOperId(idGeneratorFactory.getId(SysOperLog.class));
        operLog.setOperTime(new Date());
        operLogMapper.insert(operLog);
    }

    /**
     * 查询系统操作日志集合
     * 
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog)
    {
//        return operLogMapper.selectOperLogList(operLog);
        //<include refid="selectOperLogVo"/>
        //		<where>
        //			<if test="operIp != null and operIp != ''">
        //				AND oper_ip like concat('%', #{operIp}, '%')
        //			</if>
        //			<if test="title != null and title != ''">
        //				AND title like concat('%', #{title}, '%')
        //			</if>
        //			<if test="businessType != null">
        //				AND business_type = #{businessType}
        //			</if>
        //			<if test="businessTypes != null and businessTypes.length > 0">
        //			    AND business_type in
        //			    <foreach collection="businessTypes" item="businessType" open="(" separator="," close=")">
        //		 			#{businessType}
        //		        </foreach>
        //			</if>
        //			<if test="status != null">
        //				AND status = #{status}
        //			</if>
        //			<if test="operName != null and operName != ''">
        //				AND oper_name like concat('%', #{operName}, '%')
        //			</if>
        //			<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
        //				AND oper_time &gt;= #{params.beginTime}
        //			</if>
        //			<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
        //				AND oper_time &lt;= #{params.endTime}
        //			</if>
        //		</where>
        //		order by oper_id desc
        Map<String, Object> params = operLog.getParams();
        Object beginTime = params.get("beginTime");
        LocalDateTime beginTimeLocalDateTime = null;
        if(Objects.nonNull(beginTime)){
            LocalDate beginTimeLocalDate = LocalDate.parse(beginTime.toString(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            beginTimeLocalDateTime = LocalDateTime.of(beginTimeLocalDate, LocalTime.MIN);
        }
        Object endTime = params.get("endTime");
        LocalDateTime endTimeLocalDateTime = null;
        if(Objects.nonNull(endTime)){
            LocalDate endTimeLocalDate = LocalDate.parse(endTime.toString(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            endTimeLocalDateTime = LocalDateTime.of(endTimeLocalDate, LocalTime.MAX);
        }
        LambdaQueryWrapper<SysOperLog> la = Wrappers.<SysOperLog>lambdaQuery()
                .like(StringUtils.isNotBlank(operLog.getOperIp()), SysOperLog::getOperIp, operLog.getOperIp())
                .like(StringUtils.isNotBlank(operLog.getTitle()), SysOperLog::getTitle, operLog.getTitle())
                .eq(Objects.nonNull(operLog.getBusinessType()), SysOperLog::getBusinessType, operLog.getBusinessType())
                .eq(Objects.nonNull(operLog.getStatus()), SysOperLog::getStatus, operLog.getStatus())
                .like(StringUtils.isNotBlank(operLog.getOperName()), SysOperLog::getOperName, operLog.getOperName())
                .ge(Objects.nonNull(beginTimeLocalDateTime), SysOperLog::getOperTime, beginTimeLocalDateTime)
                .le(Objects.nonNull(endTimeLocalDateTime), SysOperLog::getOperTime, endTimeLocalDateTime);
        return sysOperLogMapper.selectList(la);
    }

    /**
     * 批量删除系统操作日志
     * 
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Long[] operIds)
    {
        return operLogMapper.deleteOperLogByIds(operIds);
    }

    /**
     * 查询操作日志详细
     * 
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Long operId)
    {
        return operLogMapper.selectOperLogById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog()
    {
        operLogMapper.cleanOperLog();
    }
}
